package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.ManagerInfoMapper;
import cc.rengu.igas.bomp.common.entity.ManagerInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 王成 on 2020/3/6
 */
public class ManagerInfoMapperImpl implements ManagerInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertManagerInfo(ManagerInfo managerInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertBompManagerInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, managerInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记拓展经理信息失败,instId:<{}>,signInstId:<{}>,managerId:<{}>,managerName:<{}>,iReturnCode:<{}>",
                    managerInfo.getInstId(), managerInfo.getSignInstId(), managerInfo.getManagerId(), managerInfo.getManagerName(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateManagerInfoById(ManagerInfo managerInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBompManagerInfoById");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, managerInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记拓展经理信息失败,instId:<{}>,signInstId:<{}>,managerId:<{}>,managerName:<{}>,iReturnCode:<{}>",
                    managerInfo.getInstId(), managerInfo.getSignInstId(), managerInfo.getManagerId(), managerInfo.getManagerName(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public ManagerInfo selectManagerInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompManagerInfoById");
        ManagerInfo managerInfo = new ManagerInfo();
        managerInfo.setInstId(instId);
        managerInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, managerInfo, managerInfo);
        if (0 != iReturnCode) {
            rglog.error("获取拓展经理信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return managerInfo;
    }

    @Override
    public ManagerInfo selectManagerInfoByPrimaryKey(String instId, String signInstId, String managerId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompManagerInfoByPrimaryKey");
        ManagerInfo managerInfo = new ManagerInfo();
        managerInfo.setInstId(instId);
        managerInfo.setSignInstId(signInstId);
        managerInfo.setManagerId(managerId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, managerInfo, managerInfo);
        if (0 != iReturnCode) {
            rglog.error("获取拓展经理信息失败,instId:<{}>,signInstId:<{}>,managerId:<{}>,iReturnCode:<{}>", instId, signInstId, managerId, iReturnCode);
            return null;
        }
        return managerInfo;
    }

    @Override
    public ManagerInfo selectManagerInfoByOpenId(String instId, String openId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompManagerInfoByOpenId");
        ManagerInfo managerInfo = new ManagerInfo();
        managerInfo.setInstId(instId);
        managerInfo.setOpenId(openId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, managerInfo, managerInfo);
        if (0 != iReturnCode) {
            rglog.error("获取拓展经理信息失败,instId:<{}>,openId:<{}>,iReturnCode:<{}>", instId, openId, iReturnCode);
            return null;
        }
        return managerInfo;
    }

    @Override
    public List<ManagerInfo> selectManagerInfoBySignInstId(String instId, String signInstId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompManagerInfoBySignInstId");
        ManagerInfo managerInfo = new ManagerInfo();
        managerInfo.setInstId(instId);
        managerInfo.setSignInstId(signInstId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, managerInfo, ManagerInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取拓展经理信息失败,instId:<{}>,signInstId:<{}>", instId, signInstId);
            return null;
        }
        return objectList.stream().map(item -> (ManagerInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<ManagerInfo> selectManagerInfoByAdminArea(String instId, String province, String city, String area) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompManagerInfoByAdminArea");
        ManagerInfo managerInfo = new ManagerInfo();
        managerInfo.setInstId(instId);
        managerInfo.setProvince(province);
        managerInfo.setCity(city);
        managerInfo.setArea(area);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, managerInfo, ManagerInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取拓展经理信息失败,instId:<{}>,province:<{}>,city:<{}>,area:<{}>", instId, province, city, area);
            return null;
        }
        return objectList.stream().map(item -> (ManagerInfo) item).collect(Collectors.toList());
    }


    @Override
    public List<ManagerInfo> selectManagerInfoListByCondition(String instId, String signInstId, String signInstType, String managerId, String managerName, String managerPhone) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompManagerInfoListByCondition");
        ManagerInfo managerInfo = new ManagerInfo();
        managerInfo.setInstId(instId);
        managerInfo.setSignInstId(signInstId);
        managerInfo.setSignInstType(signInstType);
        managerInfo.setManagerId(managerId);
        managerInfo.setManagerName(managerName);
        managerInfo.setManagerPhone(managerPhone);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, managerInfo, ManagerInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取拓展经理信息失败,instId:<{}>,signInstId:<{}>,signInstType:<{}>,managerId:<{}>,managerName:<{}>，managerPhone:<{}>",
                    instId, signInstId, signInstType, managerId, managerName, managerPhone);
            return null;
        }
        return objectList.stream().map(item -> (ManagerInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<ManagerInfo> selectBompManagerInfoByManagerId(String instId, String managerId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompManagerInfoByManagerId");
        ManagerInfo managerInfo = new ManagerInfo();
        managerInfo.setInstId(instId);
        managerInfo.setManagerId(managerId);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, managerInfo, ManagerInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取拓展经理信息失败,instId:<{}>,managerId:<{}>",
                    instId, managerId);
            return null;
        }
        return objectList.stream().map(item -> (ManagerInfo) item).collect(Collectors.toList());
    }


    @Override
    public PageResult selectManagerInfoPageByCondition(String instId, String signInstId, String signInstType, String managerId, String managerName, String managerPhone, String roleType, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompManagerInfoListByCondition");
        ManagerInfo managerInfo = new ManagerInfo();
        managerInfo.setInstId(instId);
        managerInfo.setSignInstId(signInstId);
        managerInfo.setSignInstType(signInstType);
        managerInfo.setManagerId(managerId);
        managerInfo.setManagerName(managerName);
        managerInfo.setManagerPhone(managerPhone);
        managerInfo.setRoleType(roleType);
        return dbsUtil.dbsFetchPageListData(sqlStr, managerInfo, ManagerInfo.class, pageInfo);
    }
}
